﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using XmlServer.ServiceContracts;
using XmlServer.ServiceImplementation;

namespace LogOperator
{
    public class Log : IOperator
    {

        public const string SERVER_NAME = "LogEvent Operator";
        public const string SERVER_VERSION = "1.0";

        public string Name
        {
            get { return SERVER_NAME; }
        }

        public string Version
        {
            get { return SERVER_VERSION; }
        }

        public string FullName
        {
            get { return String.Format("{0}_{1}", SERVER_NAME, Version); }
        }

        public string Description
        {
            get { return "This operator will log the specified message in the event log"; }
        }

        public List<IFact> Facts
        {
            get
            {
                return new List<IFact>
                {
                    new Fact { Name = "Source", IsRequired = true, Description = "The event viewer source." },
                    new Fact { Name = "Log", IsRequired = true, Description = "The event viewer log" },
                    new Fact { Name = "Message", IsRequired = true, Description = "The message to be written to the event viewer log" },
                    new Fact { Name = "EventType", IsRequired = true, Description = "The type of event.  Should be Error, FailureAudit, SuccessAudit, or Warning" },
                };
            }
        }

        public IResponse ProcessRequest(IRequest request)
        {
            Response response = new Response();
            response.Request = request;     // return the original request object
            response.Success = true;        // assume a success

            try
            {
                // get the "Facts" we need from the request
                var source = XmlServer.Helper.Utilities.GetFact(request.Facts, "Source");
                var log = XmlServer.Helper.Utilities.GetFact(request.Facts, "Log");
                var msg = XmlServer.Helper.Utilities.GetFact(request.Facts, "Message");
                var eventType = XmlServer.Helper.Utilities.GetFact(request.Facts, "EventType");

                WriteLogEvent(source, log, msg, eventType);

            }
            catch (Exception ex)
            {
                response.Facts = XmlServer.Helper.Utilities.CreateException(ex);
                response.Success = false;
            }

            response.StopProcessing = System.Environment.TickCount;
            return response;
        }

        private void WriteLogEvent(string source, string log, string msg, string eventType)
        {
            try
            {
                if (!EventLog.SourceExists(source))
                    EventLog.CreateEventSource(source, log);
            }
            catch {  }

            //eventType
            if (eventType.Equals("Error", StringComparison.InvariantCultureIgnoreCase))
                EventLog.WriteEntry(source, msg, EventLogEntryType.Error);
            else if (eventType.Equals("FailureAudit", StringComparison.InvariantCultureIgnoreCase))
                EventLog.WriteEntry(source, msg, EventLogEntryType.FailureAudit);
            else if (eventType.Equals("SuccessAudit", StringComparison.InvariantCultureIgnoreCase))
                EventLog.WriteEntry(source, msg, EventLogEntryType.SuccessAudit);
            else if (eventType.Equals("Warning", StringComparison.InvariantCultureIgnoreCase))
                EventLog.WriteEntry(source, msg, EventLogEntryType.Warning);
            else
                EventLog.WriteEntry(source, msg, EventLogEntryType.Information);

        }
    }
}
